<template>
  <v-app id="inspire">
    <v-content>
        <v-container >
          <v-snackbar
            v-model="snackbar"
          >
            {{ text }}
            <v-btn
              color="pink"
              text
              @click="snackbar = false"
            >
              Close
            </v-btn>
          </v-snackbar>
          <h1 style="text-align: center">My Meeting</h1>
            <displayList
              @wantToEatApple="inviteButton"
              @wantToAllContribute="allowContribute"
              @wantToContribute="contributeButton"
              @wantToOpenReview="openReview"
              @wantToReview="review"
              @wantToRelevance="relevance"
              @wantToAmount="amount"
              @wantToDistribute="strategy"
              @wantToPublish="publish"
              @wantToPublishSecond="publishSecond"
              :_items="myConferences"
              :_item-per-page="4"
            >
            </displayList>
          <side-bar></side-bar>
        </v-container>
    </v-content>
  </v-app>
</template>

<script>
import store from '../store'
import DisplayList from './DisplayList'
import SideBar from './Components/SideBar'
export default {
  name: 'Workspace',
  components: {
    SideBar,
    DisplayList
  },
  data () {
    const generateMyConference = _ => {
      const conferences = []
      this.$axios.post('/meetingIApplied', store.state.userName)
        .then(resp => {
          console.log(resp)
          console.log(resp.data)
          var response = resp.data
          response.forEach((meeting, index) => {
            conferences.push({
              identity: 'Chair',
              chair: meeting.chair,
              pcMemberNames: meeting.pcMembers,
              abbrName: meeting.abbrName,
              fullName: meeting.fullName,
              topics: meeting.topics,
              date: meeting.date,
              spot: meeting.spot,
              submitDueDate: meeting.submitDueDate,
              resultReleaseDate: meeting.resultReleaseDate,
              statusMessage: meeting.status === 0 ? 'To Be Approved'
                : (meeting.status === 1 ? 'Already Approved'
                  : (meeting.status === 2 ? 'Allow Contribution'
                    : (meeting.status === 3 ? 'Reviewing'
                      : (meeting.status === 4 ? 'Open for Rebuttal'
                        : (meeting.status === 5 ? 'Second Discussion'
                          : (meeting.status === 6 ? 'Showing final results' : 'Rejected')))))),
              router: meeting.status === -1 ? '' : 'invite/' + meeting.fullName
            })
          })
        })
      this.$axios.post('/meetingIEnded', store.state.userName)
        .then(resp => {
          var response = resp.data
          response.forEach((meeting, index) => {
            conferences.push({
              identity: 'Chair',
              chair: meeting.chair,
              pcMemberNames: meeting.pcMembers,
              abbrName: meeting.abbrName,
              fullName: meeting.fullName,
              topics: meeting.topics,
              date: meeting.date,
              spot: meeting.spot,
              submitDueDate: meeting.submitDueDate,
              resultReleaseDate: meeting.resultReleaseDate,
              statusMessage: meeting.status === 0 ? 'To Be Approved'
                : (meeting.status === 1 ? 'Already Approved'
                  : (meeting.status === 2 ? 'Allow Contribution'
                    : (meeting.status === 3 ? 'Reviewing'
                      : (meeting.status === 4 ? 'Open for Rebuttal'
                        : (meeting.status === 5 ? 'Second Discussion'
                          : (meeting.status === 6 ? 'Showing final results' : 'Rejected')))))),
              router: ''
            })
          })
        })

      this.$axios.post('/meetingIJoined', store.state.userName)
        .then(resp => {
          var response = resp.data
          response.forEach((meeting, index) => {
            conferences.push({
              identity: 'PC Member',
              chair: meeting.chair,
              pcMemberNames: meeting.pcMembers,
              abbrName: meeting.abbrName,
              fullName: meeting.fullName,
              topics: meeting.topics,
              date: meeting.date,
              spot: meeting.spot,
              submitDueDate: meeting.submitDueDate,
              resultReleaseDate: meeting.resultReleaseDate,
              statusMessage: meeting.status === 0 ? 'To Be Approved'
                : (meeting.status === 1 ? 'Already Approved'
                  : (meeting.status === 2 ? 'Allow Contribution'
                    : (meeting.status === 3 ? 'Reviewing'
                      : (meeting.status === 4 ? 'Open for Rebuttal'
                        : (meeting.status === 5 ? 'Second Discussion'
                          : (meeting.status === 6 ? 'Showing final results' : 'Rejected')))))),
              router: ''
            })
          })
        })
        .catch(error => {
          console.log(error)
        })
      return conferences
    }
    const generateInvitedConference = _ => {
      const conferences = []
      this.$axios.post('/meetingInvitedMe', store.state.userName)
        .then(resp => {
          if (resp != null) {
            var response = resp.data
            response.forEach((meeting, index) => {
              conferences.push({
                chair: meeting.chair,
                pcMemberNames: meeting.pcMembers,
                abbrName: meeting.abbrName,
                fullName: meeting.fullName,
                topics: meeting.topics,
                date: meeting.date,
                spot: meeting.spot,
                submitDueDate: meeting.submitDueDate,
                resultReleaseDate: meeting.resultReleaseDate,
                statusMessage: meeting.status === 0 ? 'To Be Approved'
                  : (meeting.status === 1 ? 'Already Approved'
                    : (meeting.status === 2 ? 'Allow Contribution'
                      : (meeting.status === 3 ? 'Reviewing'
                        : (meeting.status === 4 ? 'Open for Rebuttal'
                          : (meeting.status === 5 ? 'Second Discussion'
                            : (meeting.status === 6 ? 'Showing final results' : 'Rejected')))))) })
            })
            return conferences
          } else return null
        })
        .catch(error => {
          console.log(error)
        })
      return conferences
    }
    return {
      method: '',
      // userName: store.state.userName,
      userName: store.state.userName,
      // invitationTitle: "NewInvitations",
      contributionTitle: 'Contribution',
      router: '',
      itemsPerPage: 4,
      myConferences: generateMyConference(),
      invitedConferences: generateInvitedConference(),
      // contributedConferences :generateContributedConference(),
      snackbar: false,
      dialog: false,
      text: ''
    }
  },
  methods: {
    openApplyPage: function () {
      this.$router.replace({path: '/applyMeeting'})
    },

    allowContribute (item) {
      this.$axios.post('/allowContribute', item.fullName)
        .then(resp => {
          if (resp.status === 200 && resp.data.hasOwnProperty('abbrName')) {
            this.text = 'successful allowance'
            this.snackbar = true
            item.statusMessage = 'Allow Contribution'
            location.reload()
          } else {
            this.text = 'allow error'
            this.snackbar = true
          }
        })
        .catch(error => {
          console.log(error)
          this.text = 'allow action is not committed'
          this.snackbar = true
        })
    },

    strategy (method) {
      this.method = method
    },
    publish (item) {
      console.log(item.fullName)
      this.$axios.post('/closeReview', item.fullName)
        .then(resp => {
          if (resp.status === 200) {
            console.log(resp.data)
            if (resp.data === true) {
              this.text = '关闭审稿成功'
              this.snackbar = true
              location.reload()
            } else {
              this.text = 'pc member还没全部审核完毕'
              this.snackbar = true
            }
          } else {
            this.text = 'close error'
            this.snackbar = true
          }
        })
        .catch(error => {
          this.text = 'error!'
          this.snackbar = true
        })
    },
    publishSecond (item) {
      console.log(item.fullName)
      this.$axios.post('/showSecondResult', item.fullName)
        .then(resp => {
          if (resp.status === 200) {
            console.log(resp.data)
            if (resp.data === true) {
              this.text = '发布最终结果成功'
              this.snackbar = true
              location.reload()
            } else {
              this.text = 'pc member还没全部审核完毕或contributor还未全部确认是否提交rebuttal'
              this.snackbar = true
            }
          } else {
            this.text = '发布最终结果失败'
            this.snackbar = true
          }
        })
        .catch(error => {
          this.text = 'error!'
          this.snackbar = true
        })
    },
    relevance (item) {
      this.method = 'relevance'
      this.$axios.post('/distribute', {
        username: store.state.userName,
        meetingFullName: item.fullName,
        method: this.method
      })
        .then(resp => {
          if (resp.status === 200) {
            var response = resp.data
            if (response === true) {
              this.$axios.post('/openReview', item.fullName
              )
                .then(res => {
                  if(res.data === true){
                    console.log("resp.data:"+res.data)
                    this.text = 'successful opening'
                    this.snackbar = true
                    item.statusMessage = 'Reviewing'
                  } else {
                    this.text = '分配失败'
                    this.snackbar = true
                  }
                })
                .catch(error => {
                })
              // location.reload()
            } else {
              this.text = '分配失败'
              this.snackbar = true
              // location.reload()
            }
          } else {
            this.text = 'distribute error'
            this.snackbar = true
            // location.reload()
          }
        })
        .catch(error => {
        console.log(error)
        this.text = 'allow action is not committed'
        this.snackbar = true
        location.reload()
      })
    },
    amount (item) {
      this.method = 'amount'
      this.$axios.post('/distribute', {
        username: store.state.userName,
        meetingFullName: item.fullName,
        method: this.method
      })
        .then(resp => {
          if (resp.status === 200) {
            var response = resp.data
            if (response === true) {
              this.$axios.post('/openReview', item.fullName
              )
                .then(res => {
                  if(res.data === true){
                    this.text = 'successful opening'
                    this.snackbar = true
                    item.statusMessage = 'Reviewing'
                  } else {
                    this.text = '分配失败'
                    this.snackbar = true
                  }
                })
                .catch(error => {
                })
              // location.reload()
            } else {
              this.text = '分配失败'
              this.snackbar = true
              // location.reload()
            }
          } else {
            this.text = 'distribute error'
            this.snackbar = true
            // location.reload()
          }
        })
        .catch(error => {
          console.log(error)
          this.text = 'allow action is not committed'
          this.snackbar = true
          location.reload()
        })
    },
    openReview (item) {
      this.$axios.post('/openReview', item.fullName
      )
        .then(resp => {
          if (resp.status === 200) {
            var response = resp.data
            if (response === true) {
              this.text = 'successful opening'
              this.snackbar = true
              item.statusMessage = 'Reviewing'
              location.reload()
            } else {
              this.text = '少于两人～'
              this.snackbar = true
            }
          } else {
            this.text = 'open review error'
            this.snackbar = true
          }
        })
        .catch(error => {
          console.log(error)
          this.text = 'allow action is not committed'
          this.snackbar = true
        })
    },

    inviteButton (item) {
      console.log(item.router)
      this.$router.replace({path: item.router})
    },
    contributeButton (item) {
      console.log(item.router)
      this.$router.replace('/contribute/' + item.fullName)
    },
    update (item) {
      this.$router.replace('/workspace/myContribution/' + item.FileTitle)
    },
    review (item) {
      this.$router.replace('/workspace/review/' + item.fullName + '?' + item.FileTitle)
    }
  }
}
</script>
